/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tesis.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tesis.common.utils.CollectionsUtil;
import tesis.domain.Rol;
import tesis.domain.dto.RolDTO;
import tesis.service.RolService;
import tesis.service.CrudService;
import tesis.utils.TesisStringUtils;
/**
 *
 * @author hugo
 */
@Transactional
@Service("rolService")
public class RolServiceImpl implements RolService{
    private final static Logger LOG=LoggerFactory.getLogger(RolServiceImpl.class);
    @Inject
    private CrudService crud;
    
    
    public List<RolDTO> find(RolDTO filtro) {
        List<RolDTO> lstRoles=new ArrayList<RolDTO>();
        List<Rol>results=null;
        Query query=null;
        try{
            LOG.info("buscando roles");
            query=construirFindQuery(filtro,false);
            results=query.getResultList();
            if (!CollectionsUtil.isEmpty(results)){
                for(Rol c:results){
                    lstRoles.add(new RolDTO(c));
                }
            }
            LOG.info("lista obtenida con éxito, tam: "+lstRoles.size());
        }catch(RuntimeException ex){
            LOG.error("Error al obtener roles",ex);
            throw ex;
        }
        return lstRoles;
    }
    
    
        private Query construirFindQuery(RolDTO filtro,boolean contar){
        Query query=null;
        try{
            StringBuilder sb=new StringBuilder();

            if(!contar){
                sb.append("SELECT c ");
            }else{
                sb.append("SELECT count(c) ");
            }

            sb.append(" FROM Rol c where c.esactivo!='E' ");
            if (TesisStringUtils.conValor(filtro.getNombre())){
                sb.append(" AND UPPER(c.nombre) like :nombre ");
            }

            if (!contar){
                sb.append(" ORDER BY c.nombre asc ");
            }   

            query=crud.getEm().createQuery(sb.toString());
            if (TesisStringUtils.conValor(filtro.getNombre())){
                query.setParameter("nombre", "%"+filtro.getNombre().trim().toUpperCase()+"%");
            }

            if (!contar && filtro.getStartIndex()!=null){
                query.setFirstResult(filtro.getStartIndex());
                query.setMaxResults(filtro.getResults());
            }
            
        }catch(RuntimeException ex){
             LOG.error("Error contar rol",ex);
            throw ex;
        }
        return query;
    }
        
        
    @Override
    public Integer cantidadRoles(RolDTO filtro) {
        try{
            LOG.info("contando cargos");
           return ((Long)construirFindQuery(filtro,true).getSingleResult()).intValue();
            
        }catch(RuntimeException ex){
            LOG.error("Error contar cargos",ex);
            throw ex;
        }
        
    }
        
    
}
